package viewframework;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.MouseListener;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;


import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JToolBar;
import javax.swing.SpringLayout;
import javax.swing.SwingConstants;
import javax.swing.border.Border;

public class UIFrameWork 
{	
	public static JToolBar addToolBar(JPanel parentpanel,SpringLayout springlayout)
	{
		JToolBar toolBar = new JToolBar();
		toolBar.setBorder(null);
		toolBar.setOrientation(SwingConstants.VERTICAL);
		springlayout.putConstraint(SpringLayout.SOUTH, toolBar, 0, SpringLayout.SOUTH, parentpanel);
		springlayout.putConstraint(SpringLayout.NORTH, toolBar, 0, SpringLayout.NORTH, parentpanel);
		springlayout.putConstraint(SpringLayout.WEST, toolBar, 0, SpringLayout.WEST, parentpanel);
		springlayout.putConstraint(SpringLayout.EAST, toolBar, 180, SpringLayout.WEST,parentpanel);
		parentpanel.add(toolBar);
		return toolBar;		
	}
	
	//*****************************************************************************
	public static JLabel[] setLeftMenu(String[]menunames,JToolBar toolbar,Dimension maximumSize)
	{
		JLabel[]label=new JLabel[menunames.length];
		
		for(int i=0;i<label.length;i++)
		{
			label[i]=new JLabel(menunames[i],JLabel.CENTER);
			label[i].setMaximumSize(maximumSize);
			toolbar.add(label[i]);
			toolbar.addSeparator();
			// GET PARENT OF TOOL BAR WHICH IS OUR MOUSE LISTENER
		   MouseListener parent=(MouseListener) toolbar.getParent();
	
		   label[i].addMouseListener(parent);
		}
		
		return label;
	}
	
	//*****************************************************************************
	public static void styleLeftMenu(JLabel[]labels,Font f,Color backGround,Color foreGround)
	{
		for(int i=0;i<labels.length;i++)
		{
			
			if(f!=null)
				labels[i].setFont(f);
			if(backGround!=null)
				labels[i].setBackground(backGround);
			if(foreGround!=null)
				labels[i].setForeground(foreGround);		
		}
		
	}
	
	//*****************************************************************************
	public static void setReportComponent(SpringLayout springLayout,JPanel panel,JPanel parentPanel,int componentCount)
	{
		if(parentPanel.getComponentCount()==2)
			parentPanel.remove(1);
		
		springLayout.putConstraint(SpringLayout.SOUTH, panel, 0, SpringLayout.SOUTH,parentPanel);
		springLayout.putConstraint(SpringLayout.WEST, panel, 180, SpringLayout.WEST,parentPanel);
		springLayout.putConstraint(SpringLayout.EAST, panel, 0, SpringLayout.EAST, parentPanel);
		springLayout.putConstraint(SpringLayout.NORTH, panel, 0, SpringLayout.NORTH, parentPanel);
		parentPanel.add(panel);
		parentPanel.revalidate();
		parentPanel.repaint();
	}
	
	//*****************************************************************************
	public static void setFormComponent(SpringLayout springLayout,JPanel panel,JPanel parentPanel,int componentCount)
	{
		if(parentPanel.getComponentCount()==componentCount)
			parentPanel.remove(componentCount-1);
		
		springLayout.putConstraint(SpringLayout.SOUTH, panel, -3, SpringLayout.SOUTH, parentPanel);
		springLayout.putConstraint(SpringLayout.HORIZONTAL_CENTER, panel, 0, SpringLayout.HORIZONTAL_CENTER, parentPanel);
		springLayout.putConstraint(SpringLayout.VERTICAL_CENTER, panel, 0, SpringLayout.VERTICAL_CENTER, parentPanel);
		springLayout.putConstraint(SpringLayout.NORTH, panel, 3, SpringLayout.NORTH, parentPanel);
		parentPanel.add(panel);
		parentPanel.revalidate();
		parentPanel.repaint();
	}
	
	//*****************************************************************************
	public static JLabel[] makeHeaderMenu(JPanel parentFrame,String[]labelNames,int leftdistance,int horizontalgap,int verticalGap,Dimension[]prefereedSize)
	{
		JLabel[] headerMenu=new JLabel[labelNames.length];
		// initalize Array;
		for(int i=0;i<headerMenu.length;i++)
		{
			headerMenu[i]=new JLabel(labelNames[i],JLabel.CENTER);
		    headerMenu[i].setSize(prefereedSize[i]);
		}
		
		// initialize left component
		SpringLayout springlayout=(SpringLayout) parentFrame.getLayout();
		springlayout.putConstraint(SpringLayout.WEST, headerMenu[0],leftdistance , SpringLayout.WEST, parentFrame);
		
		// set left constrains
		for(int i=1;i<headerMenu.length;i++)
			springlayout.putConstraint(SpringLayout.WEST, headerMenu[i], horizontalgap, SpringLayout.EAST, headerMenu[i-1]);
		
		
		//for south constrain from headerSeprator
		JSeparator headerSeparator=(JSeparator) parentFrame.getComponent(0);
		for(int i=0;i<headerMenu.length;i++)
		{
			springlayout.putConstraint(SpringLayout.SOUTH, headerMenu[i], verticalGap, SpringLayout.NORTH,headerSeparator);
		    parentFrame.add(headerMenu[i]);
		}
		return headerMenu;
	}
	
	//*****************************************************************************
	public static void headerMenuStyling(JLabel[] headerMenu,int cursorType,Color fgcolortype,Border border,Color bgcolortype,Font font)
	{
		// change Cursor and fonts color
		for(int i=0;i<headerMenu.length;i++)
		{
			headerMenu[i].setCursor(Cursor.getPredefinedCursor(cursorType));
			headerMenu[i].setForeground(fgcolortype);
			headerMenu[i].setBorder(border);
			headerMenu[i].setOpaque(true);
			headerMenu[i].setBackground(bgcolortype);
			headerMenu[i].setFont(font);
		}
	}
	
	//*****************************************************************************
	// Add Listeners
	public static void addListeners(JLabel[] headerMenu,MouseListener ml)
	{
		for(int i=0;i<headerMenu.length;i++)
			headerMenu[i].addMouseListener(ml);
	}
	
	public static void stickPanel(JPanel parentPanel,JPanel childPanel,int maxCount)
	{
		if(parentPanel.getComponentCount()==maxCount)
			parentPanel.remove(maxCount-1);
		
		SpringLayout sl=(SpringLayout) parentPanel.getLayout();
		
		sl.putConstraint(SpringLayout.WEST, childPanel, 0, SpringLayout.WEST, parentPanel);
		sl.putConstraint(SpringLayout.EAST, childPanel, 0, SpringLayout.EAST, parentPanel);
		sl.putConstraint(SpringLayout.SOUTH, childPanel, -2, SpringLayout.SOUTH,parentPanel.getComponent(1));
		sl.putConstraint(SpringLayout.NORTH, childPanel, 2, SpringLayout.NORTH,parentPanel.getComponent(0));
		parentPanel.add(childPanel);
		parentPanel.revalidate();
		parentPanel.repaint();	
	}
	//*****************************************************************************
}
